package com.devMode.Factory.FactoryFunction;

/**
 * 工厂方法模式
 * 定义一个创建对象的接口，但是由子类决定要实例化哪个类，工厂方法把实例化推迟到子类
 */
public class FactoryFunction {
    public static void main(String[] args) {
        Factory factory1 = new product1Factory();
        Factory factory2 = new product2Factory();

        Product product1 = factory1.factoryMethod();

        Product product2 = factory2.factoryMethod();

        product1.show();
        product2.show();
    }
}

interface Product{
    void show();
}

class Product1 implements Product{

    @Override
    public void show() {
        System.out.println("product 1");
    }
}

class Prorudct2 implements  Product{

    @Override
    public void show() {
        System.out.println("product 2");
    }
}

/**
 * 定义一个工厂，并提供方法模板给子工厂使用
 */
abstract class Factory{
    abstract public Product factoryMethod();

}

class product1Factory extends Factory{

    @Override
    public Product factoryMethod() {
        return new Product1();
    }
}

class product2Factory extends Factory{

    @Override
    public Product factoryMethod() {
        return new Prorudct2();
    }
}


